<?php

	$pilot = GetPilotById ($user_status_thrown);


	$banned_r = mysql_query("select max(UNIX_TIMESTAMP(endtime)) as unbantime from banned where nick_mask LIKE '".$pilot[callsign]."'");

	if (mysql_num_rows($banned_r)>0) {

    	$banned = mysql_fetch_assoc($banned_r);
    	if ($banned[unbantime] >= time())
			$isbanned = 1;

    }

    $banned_r = mysql_query("select * from pilots where id='".$loginstatus."'");
    $banned = mysql_fetch_assoc($banned_r);
    if ($banned[WarnLevel] > $VIEW[WarnLevel])
        $isbanned=1;

	if ($_POST[dosend] and $_POST[poststring]) {

        if(!$isbanned) {

        	if ($_POST[posttoname])
            	$posttoname = " TO ".$_POST[posttoname];

			$isbanned = 0;

            $r = mysql_query("select id,sended,Date0,UNIX_TIMESTAMP(Date0) as time from webchat where callsign LIKE '".$pilot[callsign]."' order by id DESC LIMIT 0,1");

			$exist_r = mysql_query("select id from pilots where callsign='".$_POST[posttoname]."'");
			$exist = mysql_fetch_assoc($exist_r);
            $exist_r = mysql_query("select pilotid from activeusers where pilotid='".$exist[id]."'");

			//user has rows
            if (mysql_num_rows($r) > 0) {

            	$cstring = mysql_fetch_assoc($r);

				//all messages from user are parsed, lets check time
            	if ($cstring[sended] and (mysql_num_rows($exist_r)>0 or !$_POST[posttoname])) {
					mysql_query("insert into webchat(callsign,chatstring) values('".$pilot[callsign]."','".$_POST[poststring].$posttoname."')");
                }

            }
            else {
            	if ( (mysql_num_rows($exist_r)>0 or !$_POST[posttoname]))
	                mysql_query("insert into webchat(callsign,chatstring) values('".$pilot[callsign]."','".$_POST[poststring].$posttoname."')");
            }

        }

    }
    elseif ($_POST[dorefresh]) {




    }

		$TPmain = new TemplateHandler(ChatPage);

		$commonstat = GetCommonStat();
		$TPmain->AssignTag("<!--AWinDeath-->",$commonstat[A][kd]);
		$TPmain->AssignTag("<!--BWinDeath-->",$commonstat[B][kd]);
		$TPmain->AssignTag("<!--AAirLost-->",$commonstat[A][airlost]);
		$TPmain->AssignTag("<!--BAirLost-->",$commonstat[B][airlost]);
		$TPmain->AssignTag("<!--APilotLost-->",$commonstat[A][pilotlost]);
		$TPmain->AssignTag("<!--BPilotLost-->",$commonstat[B][pilotlost]);

		$TPmain->AssignTag("<!--AOnline-->",$commonstat[A][online]);
		$TPmain->AssignTag("<!--BOnline-->",$commonstat[B][online]);

		$mapstat = GetMapStat ();
		$TPmain->AssignTag("<!--MapName-->",$mapstat[name]);
		$TPmain->AssignTag("<!--MapHref-->",$mapstat[href]);
		$TPmain->AssignTag("<!--MapTimeLeft-->",$mapstat[minNsec]);

        $chat = GetLastChatStrings();

		if (is_array($chat) and count($chat)>0)
        foreach ($chat as $st) {

        	$datetime = explode(" ",$st[Date0]);

            if (ereg("Chat: Server:",$st[chatstring]) and ereg(" TO ",$st[chatstring]) and !ereg("Chat: Server:".$pilot[callsign].":",$st[chatstring]))
            	continue;

            $st[chatstring] = ereg_replace("Server:","",$st[chatstring]);

            $st[chatstring] = ereg_replace("<","&lt;",$st[chatstring]);
            $st[chatstring] = ereg_replace(">","&gt;",$st[chatstring]);

        	$outst .= $datetime[1].ereg_replace("Chat:","",$st[chatstring])."\r\n<br>";


        }

		$TPmain->AssignTag("<!--Chatdate-->",$datetime[0]);

		$TPmain->AssignTag("<!--ChatStrings-->",$outst);

        if ($isbanned)
			$TPmain->AssignTag("<!--ifisbanned-->",$SLANG[$lang][profile][cantpost]);
        else
			$TPmain->AssignTag("<!--ifisbanned-->","");

        $active_r = mysql_query("select * from activeusers order by pilotid");

        $activeselect = "<option value=0>To ALL</option>\r\n";

        while ($active = mysql_fetch_assoc($active_r)) {

        	$pname = GetPilotById($active[pilotid]);
            $pname = $pname[callsign];

            if ($_POST[posttoname] == $pname)
		        $activeselect .= "<option value='$pname' selected>$pname</option>";
            else
		        $activeselect .= "<option value='$pname'>$pname</option>";

        }
		$TPmain->AssignTag("<!--activeusersselect-->",$activeselect);

?>